clear all

* Directories - chose right one
*global workspace "/Users/agnarfreyrhelgason/Dropbox/Projects/2020/Panama/ebal"
*global workspace "W:/Dropbox/Projects/2020/Panama/shared/DATA"

cd $workspace

ssc install ebalance, all replace 
help ebalance
help eststo 
help frameappend

*** France
use france_ebal.dta, clear 

 rename combinedproblem_econelite ecelite
 rename combinedproblem_citizens citizens
 
save france_ebal_temp.dta,replace

cap qui frame drop results
qui frame create results str20(sample type treat dv) est se 

foreach t of varlist treat_full treat_1month {

use france_ebal_temp.dta, clear 

qui reg  `t' female age educyrs retired
keep if e(sample)

ebalance `t' female age educyrs retired, tar(2) base(WEIGHT) wttreat maxiter(50) tolerance(0.03) keep("ebal_fr_`t'.dta") replace

svyset [pweight = _webal]
	
	foreach var of varlist v27r v51r v53r v54r v55r v57r v58r v59r ecelite citizens { 
	local i=1
		cap frame change default
		cap frame drop temp
		frame copy default temp
		frame change temp

		eststo `t'_`var': svy: reg `var' `t' 

			mat b=e(b)
			mat v=e(V)

			gen sample = "ISSP, France"
			gen type = "Main"
			gen treat = "`t'"
			gen dv = "`var'"
			gen est = b[1,1]
			local se = sqrt(v[1,1])
			gen se = `se'

			keep if _n==1
			keep sample type treat dv est se
			
			frame change results
			frameappend temp
			cap frame drop temp
			frame change default

		foreach sub of varlist top40 pr_socialist pol_sophistication_binary  {	
			cap frame drop temp
			
			eststo `t'_`var'_`i': svy: reg `var' `t'##i.`sub'
			local i=`i'+1
			margins, dydx(`t') over(`sub') post
			frame copy default temp
			frame change temp

				mat b=e(b)
				mat v=e(V)

				gen sample = "ISSP, France"
				gen type = "`sub', 0"
				gen treat = "`t'"
				gen dv = "`var'"
				gen est = b[1,3]
				local se = sqrt(v[3,3])
				gen se = `se'

				replace type = "`sub', 1" if _n==2
				replace est = b[1,4] if _n==2
				local se = sqrt(v[4,4])
				replace se = `se' if _n==2

				keep if _n==1 | _n==2
				keep sample type treat dv est se

				frame change results
				frameappend temp
				cap frame drop temp

			cap frame change default
			cap frame drop temp
			frame copy default temp
			frame change temp

			svy: reg `var' `t'##i.`sub'
			margins, dydx(`t') over(r.`sub') post
				mat b=e(b)
				mat v=e(V)
				
				gen sample = "ISSP, France"
				gen type = "`sub', dx"
				gen treat = "`t'"
				gen dv = "`var'"
				gen est = b[1,2]
				local se = sqrt(v[2,2])
				gen se = `se'

				keep if _n==1
				keep sample type treat dv est se

				frame change results
				frameappend temp
				cap frame drop temp
				frame change default
			}
		}

	}


	
	
	* Spain
	use spain_ebal.dta, clear 
	
foreach t of varlist treat_full treat_2day treat_1day {

	use spain_ebal.dta, clear 

	 reg `t' /// 
		college churchattend_never unemployed smalltown region_andalucia region_canary region_castilla_lamancha /// 
		region_castile_leon region_catalonia region_valencia region_galicia ///
		region_madrid region_basque
		keep if e(sample)

	ebalance `t' /// 
		college churchattend_never unemployed smalltown region_andalucia region_canary region_castilla_lamancha /// 
		region_castile_leon region_catalonia region_valencia region_galicia ///
		region_madrid region_basque, tar(2) maxiter(50) tolerance(0.03) keep("ebal_es_`t'.dta") replace
	
	svyset [pweight = _webal]
	
foreach var of varlist likelyvote_ciudadanos likelyvote_podemos ///
				likelyvote_psoe likelyvote_pp { 				
		
	cap frame change default
	cap frame drop temp
	frame copy default temp
	frame change temp
	
	local v=substr("`var'",12,14)
	eststo sp_`t'_`v': svy: reg `var' `t' 

		mat b=e(b)
		mat v=e(V)
		
		gen sample  = "CIS, Spain"
		gen type = "Main"
		gen treat = "`t'"
		gen dv = "`var'"
		gen est = b[1,1]
		local se = sqrt(v[1,1])
		gen se = `se'


	keep if _n==1
	keep sample type treat dv est se
	
	frame change results
	frameappend temp
	cap frame drop temp
	frame change default
		
	frame copy default temp
	frame change temp
	
	eststo sp_`t'_`v'_pp: svy: reg `var' `t'##i.voted15_pp 
	margins, dydx(`t') over(voted15_pp) post

		mat b=e(b)
		mat v=e(V)
		
		gen sample  = "CIS, Spain"
		gen type = "Voted PP, 0"
		gen treat = "`t'"
		gen dv = "`var'"
		gen est = b[1,3]
		local se = sqrt(v[3,3])
		gen se = `se'
		
		replace type = "Voted PP, 1" if _n==2
		replace est = b[1,4] if _n==2
		local se = sqrt(v[4,4])
		replace se = `se' if _n==2

		keep if _n==1 | _n==2
		keep sample type treat dv est se

		
	frame change results
	frameappend temp
	cap frame drop temp
	frame change default
		
	frame copy default temp
	frame change temp
		
	svy: reg `var' `t'##i.voted15_pp 
	margins, dydx(`t') over(r.voted15_pp) post

		mat b=e(b)
		mat v=e(V)
		
		gen sample  = "CIS, Spain"
		gen type = "Voted PP, dx"
		gen treat = "`t'"
		gen dv = "`var'"
		gen est = b[1,2]
		local se = sqrt(v[2,2])
		gen se = `se'

		keep if _n==1 | _n==2
		keep sample type treat dv est se
	
	frame change results
	frameappend temp
	cap frame drop temp
	frame change default
		
	}
	
	foreach var of varlist vote_Ciudadanos vote_Podemos vote_PSOE {

	cap frame change default
	cap frame drop temp
	frame copy default temp
	frame change temp

	svy: logit `var' `t' 
	margins, dydx(`t') post

		mat b=e(b)
		mat v=e(V)

		gen sample  = "CIS, Spain"
		gen type = "Main"
		gen treat = "`t'"
		gen dv = "`var'"
		gen est = b[1,1]
		local se = sqrt(v[1,1])
		gen se = `se'

	keep if _n==1
	keep sample type treat dv est se

	frame change results
	frameappend temp
	cap frame drop temp
	frame change default
								
		
	}

	foreach var of varlist vote_PP ///
			combinedproblem_corruption firstproblem_corruption {

	cap frame change default
	cap frame drop temp
	frame copy default temp
	frame change temp

	svy: logit `var' `t' 
	margins, dydx(`t') post

		mat b=e(b)
		mat v=e(V)

		gen sample  = "CIS, Spain"
		gen type = "Main"
		gen treat = "`t'"
		gen dv = "`var'"
		gen est = b[1,1]
		local se = sqrt(v[1,1])
		gen se = `se'

	keep if _n==1
	keep sample type treat dv est se

	frame change results
	frameappend temp
	cap frame drop temp
	frame change default
								
	frame copy default temp
	frame change temp
	
	svy: logit `var' `t'##i.voted15_pp 
	margins, dydx(`t') over(voted15_pp) post
		
		mat b=e(b)
		mat v=e(V)
		
		gen sample  = "CIS, Spain"
		gen type = "Voted PP, 0"
		gen treat = "`t'"
		gen dv = "`var'"
		gen est = b[1,3]
		local se = sqrt(v[3,3])
		gen se = `se'
				
		replace type = "Voted PP, 1" if _n==2
		replace est = b[1,4] if _n==2
		local se = sqrt(v[4,4])
		replace se = `se' if _n==2
		
		keep if _n==1 | _n==2
		keep sample type treat dv est se
	
		frame change results
		frameappend temp

	cap frame drop temp
	frame change default

	frame copy default temp
	frame change temp

	svy: logit `var' `t'##i.voted15_pp 
	margins, dydx(`t') over(r.voted15_pp) post

		mat b=e(b)
		mat v=e(V)
		
		gen sample  = "CIS, Spain"
		gen type = "Voted PP, dx"
		gen treat = "`t'"
		gen dv = "`var'"
		gen est = b[1,2]
		local se = sqrt(v[2,2])
		gen se = `se'
		
		keep if _n==1
		keep sample type treat dv est se

		frame change results
		frameappend temp
		cap frame drop temp
		frame change default
		
	}
	
	
	}

frame change results
save ebal_results_new.dta, replace


*try with binary education instead for France - so both have binary:


cap qui frame drop results
qui frame create results str20(sample type treat dv) est se 


foreach t of varlist treat_full treat_1month {

use france_ebal_temp.dta, clear 

qui reg  `t' female age tertiary retired
keep if e(sample)

ebalance `t' female age tertiary retired, tar(2) base(WEIGHT) wttreat maxiter(50) tolerance(0.03) keep("ebal_fr_`t'.dta") replace

svyset [pweight = _webal]
	
	foreach var of varlist v27r v51r v53r v54r v55r v57r v58r v59r ecelite citizens { 
	local i=1
		cap frame change default
		cap frame drop temp
		frame copy default temp
		frame change temp

		eststo `t'_`var': svy: reg `var' `t' 

			mat b=e(b)
			mat v=e(V)

			gen sample = "ISSP, France"
			gen type = "Main"
			gen treat = "`t'"
			gen dv = "`var'"
			gen est = b[1,1]
			local se = sqrt(v[1,1])
			gen se = `se'

			keep if _n==1
			keep sample type treat dv est se
			
			frame change results
			frameappend temp
			cap frame drop temp
			frame change default

		foreach sub of varlist top40 pr_socialist pol_sophistication_binary  {	
			cap frame drop temp
			
			eststo `t'_`var'_`i': svy: reg `var' `t'##i.`sub'
			local i=`i'+1
			margins, dydx(`t') over(`sub') post
			frame copy default temp
			frame change temp

				mat b=e(b)
				mat v=e(V)

				gen sample = "ISSP, France"
				gen type = "`sub', 0"
				gen treat = "`t'"
				gen dv = "`var'"
				gen est = b[1,3]
				local se = sqrt(v[3,3])
				gen se = `se'

				replace type = "`sub', 1" if _n==2
				replace est = b[1,4] if _n==2
				local se = sqrt(v[4,4])
				replace se = `se' if _n==2

				keep if _n==1 | _n==2
				keep sample type treat dv est se

				frame change results
				frameappend temp
				cap frame drop temp

			cap frame change default
			cap frame drop temp
			frame copy default temp
			frame change temp

			svy: reg `var' `t'##i.`sub'
			margins, dydx(`t') over(r.`sub') post
				mat b=e(b)
				mat v=e(V)
				
				gen sample = "ISSP, France"
				gen type = "`sub', dx"
				gen treat = "`t'"
				gen dv = "`var'"
				gen est = b[1,2]
				local se = sqrt(v[2,2])
				gen se = `se'

				keep if _n==1
				keep sample type treat dv est se

				frame change results
				frameappend temp
				cap frame drop temp
				frame change default
			}
		}

	}


	
	
	* Spain
	use spain_ebal.dta, clear 
	
foreach t of varlist treat_full treat_2day treat_1day {

	use spain_ebal.dta, clear 

	 reg `t' /// 
		college churchattend_never unemployed smalltown region_andalucia region_canary region_castilla_lamancha /// 
		region_castile_leon region_catalonia region_valencia region_galicia ///
		region_madrid region_basque
		keep if e(sample)

	ebalance `t' /// 
		college churchattend_never unemployed smalltown region_andalucia region_canary region_castilla_lamancha /// 
		region_castile_leon region_catalonia region_valencia region_galicia ///
		region_madrid region_basque, tar(2) maxiter(50) tolerance(0.03) keep("ebal_es_`t'.dta") replace
	
	svyset [pweight = _webal]
	
foreach var of varlist likelyvote_ciudadanos likelyvote_podemos ///
				likelyvote_psoe likelyvote_pp { 				
		
	cap frame change default
	cap frame drop temp
	frame copy default temp
	frame change temp
	
	local v=substr("`var'",12,14)
	eststo sp_`t'_`v': svy: reg `var' `t' 

		mat b=e(b)
		mat v=e(V)
		
		gen sample  = "CIS, Spain"
		gen type = "Main"
		gen treat = "`t'"
		gen dv = "`var'"
		gen est = b[1,1]
		local se = sqrt(v[1,1])
		gen se = `se'


	keep if _n==1
	keep sample type treat dv est se
	
	frame change results
	frameappend temp
	cap frame drop temp
	frame change default
		
	frame copy default temp
	frame change temp
	
	eststo sp_`t'_`v'_pp: svy: reg `var' `t'##i.voted15_pp 
	margins, dydx(`t') over(voted15_pp) post

		mat b=e(b)
		mat v=e(V)
		
		gen sample  = "CIS, Spain"
		gen type = "Voted PP, 0"
		gen treat = "`t'"
		gen dv = "`var'"
		gen est = b[1,3]
		local se = sqrt(v[3,3])
		gen se = `se'
		
		replace type = "Voted PP, 1" if _n==2
		replace est = b[1,4] if _n==2
		local se = sqrt(v[4,4])
		replace se = `se' if _n==2

		keep if _n==1 | _n==2
		keep sample type treat dv est se

		
	frame change results
	frameappend temp
	cap frame drop temp
	frame change default
		
	frame copy default temp
	frame change temp
		
	svy: reg `var' `t'##i.voted15_pp 
	margins, dydx(`t') over(r.voted15_pp) post

		mat b=e(b)
		mat v=e(V)
		
		gen sample  = "CIS, Spain"
		gen type = "Voted PP, dx"
		gen treat = "`t'"
		gen dv = "`var'"
		gen est = b[1,2]
		local se = sqrt(v[2,2])
		gen se = `se'

		keep if _n==1 | _n==2
		keep sample type treat dv est se
	
	frame change results
	frameappend temp
	cap frame drop temp
	frame change default
		
	}
	
	foreach var of varlist vote_Ciudadanos vote_Podemos vote_PSOE {

	cap frame change default
	cap frame drop temp
	frame copy default temp
	frame change temp

	svy: logit `var' `t' 
	margins, dydx(`t') post

		mat b=e(b)
		mat v=e(V)

		gen sample  = "CIS, Spain"
		gen type = "Main"
		gen treat = "`t'"
		gen dv = "`var'"
		gen est = b[1,1]
		local se = sqrt(v[1,1])
		gen se = `se'

	keep if _n==1
	keep sample type treat dv est se

	frame change results
	frameappend temp
	cap frame drop temp
	frame change default
								
		
	}

	foreach var of varlist vote_PP ///
			combinedproblem_corruption firstproblem_corruption {

	cap frame change default
	cap frame drop temp
	frame copy default temp
	frame change temp

	svy: logit `var' `t' 
	margins, dydx(`t') post

		mat b=e(b)
		mat v=e(V)

		gen sample  = "CIS, Spain"
		gen type = "Main"
		gen treat = "`t'"
		gen dv = "`var'"
		gen est = b[1,1]
		local se = sqrt(v[1,1])
		gen se = `se'

	keep if _n==1
	keep sample type treat dv est se

	frame change results
	frameappend temp
	cap frame drop temp
	frame change default
								
	frame copy default temp
	frame change temp
	
	svy: logit `var' `t'##i.voted15_pp 
	margins, dydx(`t') over(voted15_pp) post
		
		mat b=e(b)
		mat v=e(V)
		
		gen sample  = "CIS, Spain"
		gen type = "Voted PP, 0"
		gen treat = "`t'"
		gen dv = "`var'"
		gen est = b[1,3]
		local se = sqrt(v[3,3])
		gen se = `se'
				
		replace type = "Voted PP, 1" if _n==2
		replace est = b[1,4] if _n==2
		local se = sqrt(v[4,4])
		replace se = `se' if _n==2
		
		keep if _n==1 | _n==2
		keep sample type treat dv est se
	
		frame change results
		frameappend temp

	cap frame drop temp
	frame change default

	frame copy default temp
	frame change temp

	svy: logit `var' `t'##i.voted15_pp 
	margins, dydx(`t') over(r.voted15_pp) post

		mat b=e(b)
		mat v=e(V)
		
		gen sample  = "CIS, Spain"
		gen type = "Voted PP, dx"
		gen treat = "`t'"
		gen dv = "`var'"
		gen est = b[1,2]
		local se = sqrt(v[2,2])
		gen se = `se'
		
		keep if _n==1
		keep sample type treat dv est se

		frame change results
		frameappend temp
		cap frame drop temp
		frame change default
		
	}
	
	
	}

frame change results
save ebal_results_new_binaryedu.dta, replace



esttab treat_1month_v55r treat_1month_v55r_3 treat_1month_v57r ///
treat_1month_v57r_3 using fr_tax_main.html, replace /// 
coeflabels(treat_1month "Treatment" 1.pol_sophistication_binary "Pol. Sophistication Indicator" ///
1.treat_1month#1.pol_sophistication_binary "Treatment x Pol. Sophistication") star(+ 0.1 * 0.05) ///
rename (1.treat_1month treat_1month)  drop(_cons) nodepvars ///
nobaselevels mtitles("Eq. treat" "Eq treat" "Comp. avoid" "Comp. avoid") ///
varwidth(10) nonotes b(2) se(2) r2(2) stats(N, fmt(0) labels("Observations")) ///
addnotes("OLS regression estimates calculated with entropy balancing weights." "DV in models 1-2: Tax authorities treat tax contributors equally, models 3-4: Companies try to avoid paying taxes. + p<0.1, * p<0.05.")

				
esttab treat_1month_v27r treat_1month_v27r_1 treat_1month_v51r ///
treat_1month_v51r_1 treat_1month_v53r treat_1month_v53r_1 using fr_red_main.html, replace /// 
coeflabels(treat_1month "Treatment" 1.top40 "High Income Indicator" ///
1.treat_1month#1.top40 "Treatment x High Income") star(+ 0.1 * 0.05) ///
rename (1.treat_1month treat_1month)  drop(_cons) nodepvars ///
nobaselevels mtitles("Inc. Diff" "Inc. Diff" "Hi Tax" "Hi Tax" "Low Tax" "Low Tax") ///
varwidth(30) nonotes b(2) se(2) r2(2) stats(N, fmt(0) labels("Observations")) ///
addnotes("OLS regression estimates calculated with entropy balancing weights." "DV in models 1-2: Gov. should reduce income diff., models 3-4: High income tax too high," "models 5-6: Low income tax too high. + p<0.1, * p<0.05.")

esttab treat_1month_v54r treat_1month_v54r_2 treat_1month_v57r ///
treat_1month_v57r_2 treat_1month_v58r treat_1month_v58r_2 ///
treat_1month_v59r treat_1month_v59r_2 using fr_corr_main.html, replace ///
coeflabels(treat_1month "Treatment" 1.pr_socialist "Socialist Voter" ///
1.treat_1month#1.pr_socialist "Treatment x Socialist") star(+ 0.1 * 0.05) ///
rename (1.treat_1month treat_1month)  drop(_cons) nodepvars ///
nobaselevels mtitles("Auth" "Auth" "Avoid" "Avoid" "Pol" "Pol" "Pub" "Pub") ///
varwidth(30) nonotes b(2) se(2) r2(2) stats(N, fmt(0) labels("Observations")) ///
addnotes("OLS regression estimates calculated with entropy balancing weights." "DV in models 1-2: Authorities, make people pay takes, models 3-4: Companies avoid paying taxes," "models 5-6: Politicians corrupt, models 7-8: Public officials corrupt. + p<0.1, * p<0.05.")		

esttab sp_treat_2day_ciudadanos sp_treat_2day_ciudadanos_pp sp_treat_2day_podemos ///
sp_treat_2day_podemos_pp sp_treat_2day_psoe sp_treat_2day_psoe_pp sp_treat_2day_pp ///
sp_treat_2day_pp_pp using sp_vote.html, replace  ///
coeflabels(treat_2day "Treatment" 1.treat_2day "Treatment" 1.voted15_pp "PP Voter" ///
1.treat_2day#1.voted15_pp "Treatment x PP") star(+ 0.1 * 0.05) ///
rename (1.treat_2day treat_2day)  drop(_cons) nodepvars ///
nobaselevels mtitles("Ciud" "Ciud" "Pode" "Pode" "PSOE" "PSOE" "PP" "PP") ///
varwidth(30) nonotes b(2) se(2) r2(2) stats(N, fmt(0) labels("Observations")) ///
addnotes("OLS regression estimates calculated with entropy balancing weights." "DV in models 1-2: Likely to vote Ciudadanos, models 3-4: ...to vote Podemos," "models 5-6: ...to vote PSOE, models 7-8: ...to vote PP. + p<0.1, * p<0.05.")		
